using System;
using System.Collections.Generic;
using System.Linq;
using ZilLion.Service.Core.DapperWrapper;
using ZilLion.Service.Core.DapperWrapper.SqlServer;
using ZilLion.Service.WeChatMall.Domain.Entitys.GsInfo;
using ZilLion.Service.WeChatMall.Domain.Repositorys.Interface.GoodInfo;

namespace ZilLion.Service.WeChatMall.Domain.Repositorys.GoodInfo
{
    public class GsGroupRelationShipReponsitory : SqlServerRespository<GsGroupRelationShipEntity>,
        IGsGroupRelationShipReponsitory
    {
        public GsGroupRelationShipReponsitory()
        {
            Context = new WechatMallContext();
        }

        public IList<GsGroupRelationShipEntity> GetRelationByShopid(long shopid)
        {
            var querysql = DapperHelper.CreateColSqlByMap<GsGroupRelationShipEntity>("a");
            var sql = $@"SELECT {querysql} FROM groupgsrelationship a
            INNER JOIN shopgsrelationship b ON a.gsid=b.gsid
            WHERE b.shopid=@shopid";
            var result = GetList(sql, new {shopid}).ToList();
            return result;
        }

        public void AddRelationShip(GsGroupRelationShipEntity entity, UnitOfWork unit = null)
        {
            entity.UpdatedTime = DateTime.Now;
            Add(entity, unit);
        }

        public void RemoveRelationShip(GsGroupRelationShipEntity entity, UnitOfWork unit = null)
        {
            entity.UpdatedTime = DateTime.Now;
            Remove(entity, unit);
        }

        public void RemoveByGsid(long gsid, UnitOfWork unit = null)
        {
            DapperHelper.Execute($"delete from groupgsrelationship where gsid=@gsid", Context, unit, new {gsid});
        }


        public void RemoveRelationShipByGroupid(long groupid, UnitOfWork unit = null)
        {
            var sql = $@"DELETE FROM groupgsrelationship                
            WHERE groupid=@groupid";
            DapperHelper.Execute(sql, Context, unit, new {groupid});
        }
    }
}